/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JMonkeyPanel.java
 *
 * Created on Jan 23, 2010, 3:55:00 PM
 */
package prototype.phase2.ui;

import com.jme.input.KeyInput;
import com.jme.system.DisplaySystem;
import com.jme.system.canvas.JMECanvasImplementor;
import com.jme.system.lwjgl.LWJGLSystemProvider;
import com.jmex.awt.input.AWTMouseInput;
import com.jmex.awt.lwjgl.LWJGLAWTCanvasConstructor;
import com.jmex.awt.lwjgl.LWJGLCanvas;
import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyListener;
import prototype.runtime.federate.RuntimeUser;

/**
 *
 * @author Server
 */
public class JMonkeyPanel extends javax.swing.JPanel {

    LWJGLCanvas canvas = null;
    //JMECanvasImplementor impl;
    MyControlImplementor impl;
    public int height = 600;
    public int width = 800;


    /** Creates new form JMonkeyPanel */
    public JMonkeyPanel() {
        initComponents();
        initJMonkey();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        renderPanel = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        renderPanel.setName("renderPanel"); // NOI18N

        javax.swing.GroupLayout renderPanelLayout = new javax.swing.GroupLayout(renderPanel);
        renderPanel.setLayout(renderPanelLayout);
        renderPanelLayout.setHorizontalGroup(
            renderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        renderPanelLayout.setVerticalGroup(
            renderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(renderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(renderPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel renderPanel;
    // End of variables declaration//GEN-END:variables

    private void initJMonkey() {
        /**
         * Map panel includes 3d map implemented by jmonkey
         */
        // make the canvas:
        DisplaySystem display = DisplaySystem.getDisplaySystem(LWJGLSystemProvider.LWJGL_SYSTEM_IDENTIFIER);
        display.registerCanvasConstructor("AWT", LWJGLAWTCanvasConstructor.class);
        //canvas = (LWJGLCanvas) display.createCanvas(this.getHeight(), this.getWidth());
        canvas = (LWJGLCanvas) display.createCanvas(width, height);
        canvas.setUpdateInput(true);
        canvas.setTargetRate(60);

        // add a listener... if window is resized, we can do something about
        // it.
        canvas.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent ce) {
                doResize();
            }

            private void doResize() {
                //throw new UnsupportedOperationException("Not yet implemented");
            }
        });

        // Setup key and mouse input
        KeyInput.setProvider(KeyInput.INPUT_AWT);
        KeyListener kl = (KeyListener) KeyInput.get();
        canvas.addKeyListener(kl);
        AWTMouseInput.setup(canvas, false);

        // Important! Here is where we add the guts to the panel:
        impl = new MyControlImplementor(width, height);
        canvas.setImplementor(impl);

        canvas.setBounds(0, 0, width, height);
        renderPanel.add(canvas, BorderLayout.NORTH);

        // -----------END OF GL STUFF-------------
    }

    public void setImplementorRuntimeUnit(RuntimeUser ru) {
        impl.runtimeUser = ru;
    }

    /*@Override
    public int getHeight() {
    return height;
    }

    @Override
    public int getWidth() {
    return width;
    }*/
}
